import 'package:flutter/material.dart';
import 'package:flutter_ahj_app/app/NewMine/AccountViewModel.dart';
import 'package:flutter_ahj_app/base/basewidget/view/BaseWidget.dart';
import 'package:flutter_ahj_app/base/cunchu/UserInfoUtils.dart';
import 'package:flutter_ahj_app/base/utils/JUIKit/JText.dart';
import 'package:flutter_ahj_app/base/utils/ShowFiles/zjTopBar.dart';
import 'package:flutter_ahj_app/base/utils/ToastUtils.dart';
import 'package:flutter_ahj_app/base/values/SQColor.dart';
import 'package:flutter_ahj_app/base/widget/LoadingWidget.dart';
import 'package:flutter_ahj_app/base/widget/NoneData.dart';

class ChangeSFView extends BaseStatefulWidget {
  @override
  State<StatefulWidget> initState() {
    // TODO: implement initState
    return ChangeSFViewState();
  }
}

class ChangeSFViewState extends BaseState<AccountViewModel, ChangeSFView> {

  List _data;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    Future.delayed(Duration.zero, () {
      _getData();
    });
  }

  @override
  Widget initWidget() {
    // TODO: implement initWidget
    return new Scaffold(
      backgroundColor: Color(0xfff6f7f8),
      appBar: ZJTopBar(
        elevation: 0,
        title: '切换身份',
      ).create(),
      body: _data == null ? LoadingWidget() : _data.length == 0 ? NoneData() : _bodyView(),
    );
  }

  ///获取数据
  _getData() async {
    viewModel.qyxx();
    viewModel.qyxxStream().listen((event) {
      if (event['code'] == "0") {
        setState(() {
          _data = [];
        _data = event['data'];
        });
      } else {
        ToastUtils.showToast(event['msg']);
      }
    });
  }

  ///切换身份
  _qhsfData(var tenantId) async {
    viewModel.qhsf(tenantId);
    viewModel.qhsfStream().listen((event) {
      if (event['code'] == "0") {
        UserInfoUtils.setUserToken(event['data']['accessToken']);
        ToastUtils.showToast('身份切换成功');
        Navigator.pop(context);
      } else {
        ToastUtils.showToast(event['msg']);
      }
    });
  }

  _bodyView() {
    return Container(
      margin: EdgeInsets.all(15),
      decoration: BoxDecoration(
        color: SQColor.white,
        borderRadius: new BorderRadius.all(Radius.circular(16.0)),
      ),
      child: SingleChildScrollView(
        child: ListView.builder(
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            itemCount: _data.length,
            itemBuilder: (context, index) {
              return InkWell(
                  onTap: () {
                    _qhsfData(_data[index]['id']);
                  },
                  child: Container(
                    padding: EdgeInsets.fromLTRB(15, 20, 15, 0),
                    child: Column(
                      children: [
                        Row(
                          children: [
                            Expanded(
                              child: JText(
                                _data[index]['tenantName'] ?? '',
                                color: _data[index]['selected'] ? SQColor.theme_color : SQColor.black_333333,
                                fontSize: 16,
                              ),
                            ),
                            _data[index]['selected'] ? Image.asset(
                              "img/icon_select.png",
                              width: 24,
                              height: 24,
                            ) : Container(),
                          ],
                        ),
                        Container(
                          height: 15,
                        ),
                        Container(
                          height: 1,
                          color: Color(0xfff6f6f6),
                        ),
                      ],
                    ),
                  ));
            }),
      ),
    );
  }
}
